"""
A in-memory tree representing the project graph
"""

TYPE_NAMESPACE = 0
TYPE_CLASS = 1
TYPE_FUNC = 2

class Node(object):
    def __init__(self, node_type, value, parentTree=None):
        if node_type not in [TYPE_NAMESPACE, TYPE_CLASS, TYPE_FUNC]:
            raise TypeError, "type must be one of TYPE_NAMESPACE, TYPE_CLASS, TYPE_FUNC"

        self.type = node_type
        self.value = value
        self.parentTree = parentTree
        self.sons = []

    def __repr__(self):
        t = {
                TYPE_NAMESPACE : 'namespace',
                TYPE_CLASS : 'class',
                TYPE_FUNC : 'function'
        }

        return u'<%s: %s:%s> [%s with %d sons]' % \
                (self.__class__.__name__, unicode(self), id(self), t[self.type], len(self.sons))

    def __str__(self):
        return self.value

    def __unicode__(self):
        return unicode(self.value)

    def __getitem__(self, key):
        return self.sons[key]

    def add(self, node):
        self.sons.append(node)

    def isLeaf(self):
        return len(self.sons) == 0

    def nextSon(self):
        for son in self.sons:
            yield son

